package com.easysoft.framework.action;
import com.easysoft.commons.utils.StringUtils;
import com.easysoft.commons.utils.WebConstants;
import com.easysoft.framework.annotation.logs.SystemControllerLog;
import com.easysoft.framework.beans.Page;
import com.easysoft.framework.workflow.model.SysWorkflow;
import com.easysoft.framework.workflow.model.SysWorkflowQuery;
import com.easysoft.framework.workflow.model.SysWorkflowView;
import com.easysoft.framework.workflow.service.SysWorkflowService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;
/**
 * 公文流转
 *
 */
@RestController
@RequestMapping(value = SystemWorkFlowAction.BASE_VIEW)
public class SystemWorkFlowAction {
    public static final String BASE_VIEW = "/workflow";
    private static final Logger logger = LoggerFactory.getLogger(SystemWorkFlowAction.class);
    @Autowired
    @Qualifier("service.SysWorkflowService")
    private SysWorkflowService sysWorkflowService;

    /**
     * 分页查询公文流转
     * @param page  分页信息
     * @param query 查询条件
     * @return 系统公告列表
     */
    @RequestMapping(value = "/listWorkFlow", method = {RequestMethod.POST, RequestMethod.GET})
    public ModelAndView listWorkFlow(Page page, SysWorkflowQuery query) {
        logger.info("Call SystemWorkFlowAction.listWorkFlow({},{})", page.toString(), query.toString());
        List<SysWorkflowView> list = sysWorkflowService.selectSysWorkflowByPage(page, query);
        ModelAndView mv = new ModelAndView(BASE_VIEW + "/listWorkflow");
        mv.addObject("page", page);
        mv.addObject("query", query);
        mv.addObject("listWorkFlow", list);
        return mv;
    }

    /**
     * 跳转公文流转页面
     *
     * @param id 公文流转
     */
    @RequestMapping(value = "/editSysWorkFlow")
    public ModelAndView editSysWorkFlow(String action,String id) {
        logger.info("Call SystemWorkFlowAction.editSysWorkFlow({},{})", id,action);
        ModelAndView mv = new ModelAndView(BASE_VIEW + "/editWorkflow");
        if(StringUtils.isNotNullAndBlank(id) && StringUtils.isNotNullAndBlank(action) && action.toLowerCase().equals("update")){
            SysWorkflow sysWorkflow = sysWorkflowService.selectSysWorkflowById(id);
            if (sysWorkflow == null) {
                return mv;
            }
            mv.addObject("sysWorkflow",sysWorkflow);
        }
        return mv;
    }

    /**
     * 添加公文流转
     *
     * @param sysWorkFlow 公文流转
     */
    @RequestMapping(value = "/addSysWorkFlow", method = RequestMethod.POST)
    @SystemControllerLog(description = "添加公文流转")
    public int addSysWorkFlow(String action, SysWorkflow sysWorkFlow, @RequestParam(value="multipartFile", required=false)MultipartFile multipartFile, HttpServletRequest request, HttpSession session) {
        logger.info("Call SystemWorkFlowAction.addSysWorkFlow({},{})",action,sysWorkFlow.toString());
        sysWorkFlow.setOperator((String) session.getAttribute(WebConstants.SESSION_USER_ID));

        if (StringUtils.isNotNullAndBlank(action) && action.toLowerCase().equals("add")) {

            return sysWorkflowService.addSysWorkflow(sysWorkFlow);
        }
        if (StringUtils.isNotNullAndBlank(action) && action.toLowerCase().equals("update")) {

            return sysWorkflowService.updateSysWorkflow(sysWorkFlow);
        }
        return 0;
    }
}
